<?php

namespace App\Http\Controllers\Admin;

use App\Contracts\Repositories\NewsCategoryRepository;
use App\Http\Requests\Admin\NewsCategoryCreateRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;

class NewsCategoryController extends Controller
{

    private $repository;

    public function __construct(NewsCategoryRepository $repository)
    {
        $this->repository =  $repository;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $categories = $this->repository->orderBy("id")->paginate(15);
        $categories = $this->repository->sort($categories);//对查询内容进行排序
        return view("admin.news_category.index",["categories"=>$categories]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = $this->repository->all();
        $categories = $this->repository->sort($categories);//对查询内容进行排序
        return view("admin.news_category.create",['categories'=>$categories]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\Admin\NewsCategoryCreateRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(NewsCategoryCreateRequest $request)
    {
        if($this->repository->create($request->all())){
            return Redirect::to("admin/news/category");
        }else{
            return Redirect::back()->widthInput()->withErrors("保存失败！");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

        $category = $this->repository->find($id);
        $categories = $this->repository->all();
        $categories = $this->repository->sort($categories);//对查询内容进行排序
        return view("admin.news_category.edit",["category"=>$category,"categories"=>$categories]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if($this->repository->update($request->all(),$id)){
            return Redirect::to("admin/news/category");
        }else{
            return Redirect::back()->withInput()->withErrors("更新失败！");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->repository->delete($id);
        return Redirect::to("admin/news/category")->withSuccess("删除成功！");
    }
}
